<?php
/*
Copyright 2007,2008 Benjamin V King

BH Widgets is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

BH Widgets is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with BH Widgets; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

require_once('BH_Widget_Base.php');

class BH_Pages_Widget extends BH_Widget_Base {

	function get_parents($page_id='') {
		global $wpdb, $post_ID;
		$parents = array();
	
		$page_id = $page_id ? $page_id : $post_ID;
	
		$page = get_page($page_id);
		$Pages_id = $page->post_parent;

		while ($Pages_id && ($Pages_id != $page_id)) {
			$parent = get_page($Pages_id);
			array_push($parents, $parent);
		}
	
		return $parents;
	}


	/*
	 * function display_Pages_page_links()
	 *
	 * derived from get_links_list by Dougal
	 *
	 * Parameters:
	 *  
	 */
	function display_page_links($args, $page_id = '') {
		global $id;
	
		if (!is_page()) return;
		
		parse_str($args, $r);
		extract($r);

		$page_id = $page_id ? $page_id : $id;
		$page = get_page($page_id);
	
		echo $before_item;
	
		if (!$page->post_parent) {
			echo '<a href="' . get_settings('siteurl') . '" title="Home">Home</a>';

		} else {		
			$parent = get_page($page->post_parent);
			$this->display_page_links($args,$parent->ID);
			echo '<a href="' . get_page_link($parent->ID) . '" title="' . wp_specialchars($parent->post_title) . '">' . $parent->post_title . '</a>';
		}

		echo $after_item ."\n";
		
		if ($id == $page_id) {
			echo "$before_item<span>". $page->post_title ."</span>$after_item\n";
		}
		
		return;	
	}
	

	/*
	 * function display_child_links()
	 *
	 * derived from get_links_list by
	 * added by Dougal
	 *
	 * Parameters:
	 *  
	 */
	function display_child_links($args) {
		global $id;

		parse_str($args, $r);
		extract($r);

		if (!isset($depth)) $depth = 0;
		if (!isset($show_date)) $show_date = '';
		if (!isset($child_of)) $child_of = 0;

	    if (is_page()) {
	           $child_of = $id;
		}

		// Query pages.
		$pages = get_pages($args);
	
		if ( !$pages ) return;

		// Now loop over all pages that were selected
		$page_tree = Array();
		foreach($pages as $page) {
			// set the title for the current page
			$page_tree[$page->ID]['title'] = $page->post_title;

			// set the selected date for the current page
			// depending on the query arguments this is either
			// the createtion date or the modification date
			// as a unix timestamp. It will also always be in the
			// ts field.
			if (! empty($show_date)) {
				if ('modified' == $show_date)
					$page_tree[$page->ID]['ts'] = $page->time_modified;
				else
					$page_tree[$page->ID]['ts'] = $page->time_created;
			}

			// The tricky bit!!
			// Using the parent ID of the current page as the
			// array index we set the curent page as a child of that page.
			// We can now start looping over the $page_tree array
			// with any ID which will output the page links from that ID downwards.
			$page_tree[$page->post_parent]['children'][] = $page->ID; 	
		}	
		// Output of the pages starting with child_of as the root ID.
		// child_of defaults to 0 if not supplied in the query.
		$this->level_out($child_of,$page_tree, $r);
	
	}

	/*
	 * function level_out()
	 *
	 * derived from get_links_list by
	 * added by Dougal
	 *
	 * Parameters:
	 *   order (default 'name')  - Sort link categories by 'name' or 'id'
	 */
	function level_out($parent, $page_tree, $args, $depth = 0) {
		global $wp_query;

		$queried_obj = $wp_query->get_queried_object();

		if($depth)
			$indent = str_repeat("\t", $depth);
		
			if ($page_tree[$parent]['children']) foreach($page_tree[$parent]['children'] as $page_id) 	{
				$cur_page = $page_tree[$page_id];
				$title = $cur_page['title'];
		
				if(isset($args['page_link_tag'])) {
			
					echo $indent . '<' . $args['page_link_tag'];
			
				if(isset($args['css_class'])) {
					$css_class = $args['css_class'];
				
					if( $page_id == $queried_obj->ID)
						$css_class .= "$depth";
						
					echo ' class="' . $css_class;
				}
				echo  '">';
			}
				
			echo '<a href="' . get_page_link($page_id) . '" title="' . wp_specialchars($title) . '">' . $title . '</a>';

			if(isset($cur_page['ts'])) {
				$format = get_settings('date_format');

				if(isset($args['date_format']))
					$format = $args['date_format'];

				echo " " . gmdate($format,$cur_page['ts']);
			}

			echo "\n";

			if(isset($cur_page['children']) && is_array($cur_page['children'])) {
				$new_depth = $depth + 1;

				if(!$args['depth'] || $depth < ($args['depth']-1)) {
					echo "$indent\n";
					BH_Pages_Widget::level_out($page_id,$page_tree, $args, $new_depth);
					echo "$indent\n";
				}
			}
		
			if(isset($args['page_link_tag']))
				echo "$indent</" . $args['page_link_tag'] . ">";

			if(isset($args['after_item']))
				echo $args['after_item'];

			echo "\n";

		}
	}

	/*
	 * function has_children()
	 *
	 * derived from get_links_list by
	 * added by Dougal
	 *
	 * Parameters:
	 *
	 */
	function has_children($arg='') {
		global $id;
	
		if (!$arg) $arg = $id;

		$children = get_pages('depth=1&child_of='. $arg);

		if ($children) return true;

		return false;
	}
	
	function widget($args) {
		
		if (is_page() && !$this->has_children())
			return;
			
		extract($args);

		echo "$before_widget\n";
		echo $this->get_widget_start_tag($args);
		
		echo "$before_list\n";
	    $this->display_page_links('before_item='. $before_item .'&after_item='. $after_item);
		$this->display_child_links('depth=1&before_item='. $before_item .'&after_item='. $after_item);
		echo "$after_list\n";
		
		echo $this->get_widget_end_tag($args);
		echo "$after_widget\n";

	}
}

bh_widgets_register_class('BH_Pages_Widget');
?>
